package com.zjy.software.day12;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args)
    {
//        treeSetSortComparable();

        //定制排序演示,先创建一个定制排序对象,传入到TreeSet的构造方法中,这时候的Cat就不需要实现Comparable接口
        CatComparator catComparator = new CatComparator();
        Set<Cat> cats=new TreeSet<>(catComparator);
        cats.add(new Cat("2wangcai",11));
        cats.add(new Cat("3wangcai",11));
        cats.add(new Cat("1wangcai",11));
        cats.add(new Cat("5wangcai",11));
        cats.add(new Cat("5wangcai",11));
        System.out.println("cats = " + cats);
    }



    private static void treeSetSortComparable() {
        Set<String> set = new TreeSet();
        set.add("111");
        set.add("222");
        set.add("555");
        set.add("111");
        set.add("222");
        System.out.println("set = " + set);

        Set<Dog> dogs=new TreeSet<>();
        dogs.add(new Dog("6wangcai",11));
        dogs.add(new Dog("1zhaoyun",11));
        dogs.add(new Dog("3lisi",22));
        dogs.add(new Dog("2adcdog",22));
        dogs.add(new Dog("4wangcai",11));
        System.out.println("dogs = " + dogs);
    }
}
class CatComparator implements Comparator<Cat> {
    @Override
    public int compare(Cat o1, Cat o2) {
        return o1.getName().compareTo(o2.getName());
    }
}